package com.bdqn.controller;

import com.bdqn.pojo.Storage;
import com.bdqn.pojo.Supplier;
import com.bdqn.pojo.SysRole;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.StorageService;
import com.bdqn.service.SupplierService;
import com.bdqn.util.Constants;
import com.bdqn.util.Page;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 入库记录控制类
 *
 * @aurhor Administrator  whs
 * @since 2024/7/30
 */
@Controller
@RequestMapping(value = "/storageRecord")
public class StorageController {

    @Resource
    private StorageService storageService;

    private Logger logger = Logger.getLogger(StorageController.class);


    /**
     * 查询入库记录信息
     */
    @GetMapping("/list")
    public String list(Model model, @RequestParam(value = "queryGoodsName",required = false) String queryGoodsName ,@RequestParam(value = "querySupplierId",required = false) Integer querySupplierId,@RequestParam(value = "queryPayStatus",required = false) Integer queryPayStatus ,@RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex) {

        List<Storage> storageList = null;
        Page<Storage> page = new Page(); // 构建分页对象
        try {
                // 获取所有供货商信息填充下拉列表
                List<Supplier> supplierList = storageService.selectSupplierList(querySupplierId);
                // 查询符合条件的总用户数据量
                int count = storageService.count(queryGoodsName,querySupplierId,queryPayStatus);
                System.out.println("-->  queryGoodsName = "+ queryGoodsName);
                System.out.println("-->  querySupplierId = "+ querySupplierId);
                System.out.println("-->  storageList count = "+count);
                page.setTotalCount(count); // 设置总页数
                // 先设置总数据量再设置页码
                page.setCurrentPageNo(pageIndex); // 当前页数
                int pageSize = page.getPageSize();  // 获取页面容量 5
                // 查询分页数据
                storageList = storageService.selectStoragePageBeginLimitList(queryGoodsName,querySupplierId,queryPayStatus,pageIndex,pageSize);
                page.setList(storageList); // 将对象传入分页对象
                 System.out.println( "总记录数"+ storageList.size());
                int totalPageCount = page.getTotalCount(); // 获取总页数


                model.addAttribute("queryGoodsName",queryGoodsName); //  供货商名称
                model.addAttribute("querySupplierId",querySupplierId); //  供货商编码
                model.addAttribute("queryPayStatus",queryPayStatus); //  付款状态
                model.addAttribute("page",page); // 分页对象
                model.addAttribute("supplierList", supplierList); // 获取所有供货商信息填充下拉列表
                model.addAttribute("storageRecordList", storageList); // 入库记录信息

                model.addAttribute("totalCount",totalPageCount);
                model.addAttribute("totalPageCount",pageSize);
                model.addAttribute("currentPageNo",pageIndex);

        }catch (Exception e) {
            e.printStackTrace();
           return "redirect:/frame";
        }
        return "storageRecord/list";
    }

    /**
     * 去到 用入库记录详情页面
     */
    @GetMapping("/view/{storageRecordId}")
    public String userView(Model model, @PathVariable Integer storageRecordId) {
        logger.debug("根据用户id查询入库记录的详情信息"+ storageRecordId);
        Storage storageRecord = storageService.selectStoragesById(storageRecordId);
        model.addAttribute(storageRecord);
        return "storageRecord/view";
    }


    /**
     * 去到添加入库记录页面
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model,@PathVariable Integer storageRecordId) {
        Storage storageRecord = storageService.selectStoragesById(storageRecordId);
        model.addAttribute(storageRecord);
        return "storageRecord/add";
    }

    /**
     * 添加入库记录信息
     * @param model
     * @return
     */
    @PostMapping("/add")
    public String add(Model model,HttpSession session, Storage storage){
        SysUser loginSysUser = (SysUser) session.getAttribute(Constants.USER_SESSION);
        storage.setCreatedUserId((long) loginSysUser.getId());
        storage.setCreatedTime(new Date());
        boolean flag = storageService.add(storage);
        if (flag){ // 成功重定向到列表页
            return "redirect:/storageRecord/list";
        }
        model.addAttribute("error","添加失败！");
        return "storageRecord/add";
    }


    /**
     * 跳转到修改入库记录信息页面
     */
    @GetMapping("/toUpdate/{storageRecordId}")
    public String toUpdate(@PathVariable Integer storageRecordId, Model model) {
        logger.debug("跳转到修改角色信息页面");
        Storage storageRecord = storageService.selectStoragesById(storageRecordId);
        model.addAttribute(storageRecord);
        return "storageRecord/update";
    }

    /**
     *修改入库记录信息
     */
    @PostMapping("/update")
    public String modifySave(HttpSession session, Storage storage) {
        storage.setUpdatedUserId(((SysUser)session.getAttribute(Constants.USER_SESSION)).getId());
        storage.setUpdatedTime(new Date());
        if (storageService.update(storage)){
            return "redirect:/storageRecord/list";
        }
        return "storageRecord/update";

    }


}
